-- Lua script of map Level 1/hard_maze.
-- This script is executed every time the hero enters this map.

-- Feel free to modify the code below.
-- You can add more events and remove the ones you don't need.

-- See the Solarus Lua API documentation:
-- http://www.solarus-games.org/doc/latest

local map = ...
local game = map:get_game()

local time = 0

local ceilings = {ceiling_1, ceiling_2, ceiling_3, ceiling_4, ceiling_5, ceiling_6, ceiling_7, ceiling_8, ceiling_9, ceiling_10, ceiling_11, ceiling_12, ceiling_13, ceiling_14, ceiling_15, ceiling_16, ceiling_17, ceiling_18, ceiling_19, ceiling_20, ceiling_21, ceiling_22, ceiling_23, ceiling_24, ceiling_25}

local checkers = {room_checker_1, room_checker_2, room_checker_3, room_checker_4, room_checker_5, room_checker_6, room_checker_7, room_checker_8, room_checker_9, room_checker_10, room_checker_11, room_checker_12, room_checker_13, room_checker_14, room_checker_15, room_checker_16, room_checker_17, room_checker_18, room_checker_19, room_checker_20, room_checker_21, room_checker_22, room_checker_23, room_checker_24, room_checker_25}
local exits = {exit_1, exit_2, exit_3, exit_4}
local chests = {chest_1, chest_2, chest_3, chest_4}
local end_sensors = {end_sensor_1, end_sensor_2, end_sensor_3, end_sensor_4}
local end_doors = {end_door_1, end_door_2, end_door_3, end_door_4}

local end_x = {}

local completed = false

--local count = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
math.randomseed(os.time())
local r = math.random(4)
-- Event called at initialization time, as soon as this map becomes is loaded.
function map:on_started()

  -- You can initialize the movement and sprites of various
  -- map entities here.
  for i = 1, 4 do
    exits[i]:set_enabled(false)
    --chests[i]:set_enabled(false)
    --end_sensors[i]:set_enabled(false)
   -- end_doors[i]:set_enabled(false)


    --print(chests[i]:get_treasure())
    --print(chests[i]:is_open())
  end

  map:open_doors("end_door")
  print(r)
  exits[r]:set_enabled(true)
  --chests[r]:set_enabled(true)
  end_sensors[r]:set_enabled(true)
end


function map:on_update()
  for i = 1, table.getn(ceilings) do
    if checkers[i]:overlaps(hero) then
      ceilings[i]:set_enabled(false)
    else
      ceilings[i]:set_enabled(true)
    end
  end

 
  if end_sensors[r]:overlaps(hero) then
    map:close_doors("end_door")
    completed = true
    hero:start_treasure("rupee")
  end
 
end

-- Event called after the opening transition effect of the map,
-- that is, when the player takes control of the hero.
function map:on_opening_transition_finished()
  game:start_dialog("find_exit", function()
    incr_timer()
  end)
end

function incr_timer()
  sol.timer.start(1000, function() 
    if time >= 30 then
      game:start_dialog("game.out_of_time")
      hero:teleport("Level 1/right","maze_hard_exit")
    elseif not completed then
      time = time + 1
      incr_timer()
    end
  end)
end

function print_timer()
  if not already_won then
    text = sol.text_surface.create{
      font = "alttp",
      font_size = menu_font_size,
      color = {255, 255, 255},
      text = time,
      horizontal_alignment = "center"
    }
    camera = map:get_camera()
    x,y = camera:get_position()
    map:draw_visual(text, x + 300, y + 25)
  end
end

function map:on_draw()
  print_timer()
end

function win()
end
